package web;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import dao.DaoException;

import auth.service.User;
import business.domaine.IDomaineManager;
import business.unite.IUniteEnseignementManager;
import business.unite.UniteEnseignementManagerException;
import entites.Domaine;
import entites.Etat;
import entites.UniteEnseignement;

public class UpdateUniteEnseignementController extends SimpleFormController {
	private IUniteEnseignementManager ueManager;
	private IDomaineManager domaineManager;
	Domaine  domaine ;
	String code=null;
	User user ;
	public IUniteEnseignementManager getUeManager() {
		return ueManager;
	}
	/**
	 * le setter de l'unite 
	 * @param ueManager
	 */
	public void setUeManager(IUniteEnseignementManager ueManager) {
		this.ueManager = ueManager;
	}
	/**
	 * 
	 * @return
	 */
	public IDomaineManager getDomaineManager() {
		return domaineManager;
	}

	public void setDomaineManager(IDomaineManager domaineManager) {
		this.domaineManager = domaineManager;
	}

	public UpdateUniteEnseignementController() {
		//setCommandClass(UniteEnseignement.class);
		//setCommandName("ue");
	}

	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		// Car defaultCar = new Car();
		
		UniteEnseignement ue = new UniteEnseignement();
		ue.setEtat(Etat.en_cours);
		ue.setDateDerniereModif(new Date());

		return ue;
	}

	
	@Override
	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map<Object, Object> dataMap = new HashMap<Object, Object>();
		code = request.getParameter("id");
		user=(User) request.getSession().getAttribute("user");

		dataMap.put("listDomaine", domaineManager.selectDomaines());
		if(code!=null){
			UniteEnseignement ue= ueManager.FindUEByCode(code);
			dataMap.put("ue", ue);
			
		}
		return dataMap;
	}

	
	
	@Override
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		binder.setDisallowedFields(new String[] { "domaine" });
		
		UniteEnseignement ue = (UniteEnseignement) binder.getTarget();

		Long domaineId = null;
		try {
			domaineId = Long.parseLong(request.getParameter("domaine"));
		} catch (Exception e) {
		}
		if (domaineId != null) {
			domaine = domaineManager.selectDomaineById(domaineId.intValue());
		}
		System.out.println("binder");
	}
	
	/**
	 * 
	 */
	@Override
	protected ModelAndView onSubmit(Object command) throws Exception {
		
		UniteEnseignement ue = (UniteEnseignement) command;
		ue.setDateDerniereModif(new Date());
		ue.setEtat(Etat.en_cours);

		if(ue.getCode()==null||ue.getNbCredits()==0)
			return new ModelAndView(new RedirectView("addUniteEnseignement.htm") );
		
		try{
			ueManager.insertUE(user,ue,domaine);		
		}
		catch ( DaoException e){
			return new ModelAndView(new RedirectView(getFormView()), "message", e);
		}
		catch (UniteEnseignementManagerException mess){
			return new ModelAndView(new RedirectView(getFormView()), "message", mess);

		}
		return  new ModelAndView(new RedirectView(getSuccessView()));
		
		
	}
	

	public Domaine getDomaine() {
		return domaine;
	}

	public void setDomaine(Domaine domaine) {
		this.domaine = domaine;
	}

}
